package com.mchsdk.paysdk.http.checknum;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.mchsdk.paysdk.bean.ChannelAndGameInfo;
import com.mchsdk.paysdk.config.MCHConstant;
import com.mchsdk.paysdk.http.Post;
import com.mchsdk.paysdk.http.RequestParamUtil;
import com.mchsdk.paysdk.utils.MCLog;
import com.mchsdk.paysdk.utils.ToastUtil;
/**
 *验证手机账号是否存在
 */
public class CheckRequest{

	private final String TAG = "CheckRequest";
	public	Post post;
	public	Context context;
	  /**
     *验证手机号是否存在 
     */
	public Post postIsExit(String phonenum) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("account", phonenum);
		map.put("game_id", ChannelAndGameInfo.getInstance().getGameId());
		String url = MCHConstant.getInstance().getIpAddress()+ "/User/checkAccount";
		post.post(1, url, map, callBack);
		return post;
	}
	/**
	 * 验证手机验证码是否正确
	 * @param phonecode 手机验证码
	 * @return
	 */
	public Post postIsPhoneCode(String phonecode,String phonenum,RequestCallBack<String> request){
		Map<String, String> map = new HashMap<String, String>();
		map.put("code", phonecode);
		map.put("phone",phonenum);
		map.put("game_id", ChannelAndGameInfo.getInstance().getGameId());
		String url = MCHConstant.getInstance().getIpAddress()+ "/User/verify_sms";
		post.post(2, url, map, request);
		return post;
	}
	RequestCallBack<String> callBack=new RequestCallBack<String>() {
		
		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			int resultCode = RequestParamUtil.ResultCode(post);
			String result = RequestParamUtil.Result(responseInfo);
			JSONObject json=null;
			String status="";
			String return_code="";
			String return_msg="";
			try {
				json= new JSONObject(result);
				status=json.optString("status");
				return_code=json.optString("return_code");
				return_msg=json.optString("return_msg");
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
			switch (resultCode) {
			case 1:
					if("0".equals(status)){//验签失败
						ToastUtil.show(context, return_msg);
					}else if("1".equals(status)){//可以注册
						
					}else if("2".equals(status)){
						ToastUtil.show(context, return_msg);
					}else if("-1".equals(status)){
						ToastUtil.show(context, return_msg);
					}else {
						ToastUtil.show(context, "不可以注册");
					}
		        break;
			case 2:
				if("0".equals(status)){//验签失败
					ToastUtil.show(context, return_msg);
				}else if("1".equals(status)){//数据获取失败
					
				}else if("2".equals(status)){
					ToastUtil.show(context, return_msg);
				}else if("-1".equals(status)){
					ToastUtil.show(context, return_msg);
				}else {
					ToastUtil.show(context, "不可以注册");
				}
				break;
			default:
				break;
			}
		}
		
		@Override
		public void onFailure(HttpException error, String msg) {
			MCLog.e(TAG, "onFailure " + error.getExceptionCode());
			MCLog.e(TAG, "onFailure " + msg);
			ToastUtil.show(context, "服务器开小差");
	    }
	};
}
